// pages/cart/cart.js
import { cartlist, cartNum, cartdel, login, register } from '../../utils/http'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    loginstatus: true,
    list: [],
    sum: 0,
  },
  getlist() {
    let token = wx.getStorageSync('token')
    let sum = 0;
    cartlist(token).then(res => {
      // console.log(res.code)
      if (res.code == 700) {
        this.setData({
          list: []
        })
      } else {
        res.data.items.forEach(item => {
          sum += item.number * item.price
        })
        this.setData({
          list: res.data.items,
          sum,
        })
      }
    }).catch(err => {
      console.log(err)
    })
  },
  // 数量自增改变
  change(key) {
    let token = wx.getStorageSync('token')
    let keys = key.currentTarget.dataset.key
    let num = 1
    cartlist(token).then(res => {
      res.data.items.forEach(item => {
        if (item.key === keys) {
          num = item.number
          num++;
          cartNum(`/shopping-cart/modifyNumber?key=${keys}&number=${num}&token=${token}`).then(res => {
            this.getlist()
          }).catch(err => console.log(err))
        }
      })
    })
  },
  // 数量自减改变
  changes(key) {
    let token = wx.getStorageSync('token')
    let keys = key.currentTarget.dataset.key
    let num = 1
    cartlist(token).then(res => {
      res.data.items.forEach(item => {
        if (item.key === keys) {
          num = item.number
          num--;
          if (num >= 1) {
            cartNum(`/shopping-cart/modifyNumber?key=${keys}&number=${num}&token=${token}`).then(res => {
              // console.log(res)
              this.getlist()
            }).catch(err => console.log(err))
          }

        }
      })
    })
  },
  // 删除
  remove(key) {
    let token = wx.getStorageSync('token')
    let keys = key.currentTarget.dataset.key
    cartdel(`/shopping-cart/remove?key=${keys}&token=${token}`).then(res => {
      this.getlist()
      wx.showToast({
        title: "删除成功",
        icon: 'success',
        duration: 2000
      })
    }).catch(err => console.log(err))
  },
  getUserInfo(e) {
    let that = this
    wx.login({
      success(res) {
        if (res.code) {
          // 注册
          register(res.code).then(res => {
            // console.log(res)
          }).catch(err => {
            console.log(err)
          })
        }
      }
    })
    wx.login({
      success(res) {
        if (res.code) {
          // 登录
          login(res.code).then(res => {
            // console.log(res)
            wx.setStorageSync('openid', res.data.openid)
            wx.setStorageSync('token', res.data.token)
            wx.setStorageSync('uid', res.data.uid)
            wx.setStorageSync('user', e.detail.userInfo)
            that.setData({
              userList: e.detail.userInfo,
              userID: res.data.uid,
              loginStatus: true,
              loginstatus: false,
            })
            that.getlist()
          }).catch(err => {
            console.log(err)
          })
        }
      }
    })
  },
  nologin() {

    this.setData({
      loginstatus: false,
      list:[],
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // this.getlist()
    let token = wx.getStorageSync('token')
    // console.log(token)
    // 判断是否登录
    if (token) {
      this.setData({
        loginstatus: false,
      })
      // 购物车数据获取
      this.getlist()
    } else {
      this.setData({
        loginstatus: true,
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})